#!/bin/bash
/tos/tlog -m "Grub installation start"
ROOT=/mnt/harddisk/root
mkdir -p $ROOT
#umount all /mnt/harddisk mounted if mounted
#mount | grep "$ROOT" | awk '{print $3}' | sort -r | xargs umount
if [ -z "$INSDISK" ];then
	INSDISK="sda"
fi

grep -q "rootsize=max" /proc/cmdline
if [ $? -eq 0 ]; then
        if [ -d /sys/firmware/efi ]; then
                mount /dev/${INSDISK}2 $ROOT
                mount /dev/${INSDISK}1 $ROOT/boot/efi
        else
                mount /dev/${INSDISK}1 $ROOT
        fi
else
        mount /dev/${INSDISK}1 $ROOT
        #mount /dev/${INSDISK}3 $ROOT/usr/local
        test -d /sys/firmware/efi && mount /dev/${INSDISK}2 $ROOT/boot/efi
fi


mount -o bind /dev $ROOT/dev
mount -t devpts devpts $ROOT/dev/pts
mount -t proc proc $ROOT/proc 
mount -t sysfs sysfs $ROOT/sys
#change password
#echo -n -e "Dis@init3\nDis@init3\n" | chroot $ROOT passwd
#chroot $ROOT chage -d 0 root

get_passwd() {
        # find passwd in /proc/cmdline
	grep -q "passwd=" /proc/cmdline
	if [ $? -eq 0 ]; then
        	c=$(cat /proc/cmdline);
        	on1=${c##*passwd=};
        	PASSWD=${on1%% *};
        	if [ -z "$PASSWD" ]; then
                	PASSWD=0
        	fi
	else
		PASSWD=0
	fi

}

#recovery-mode remove /boot/*backup-recovery*
grep -q "recovery-mode" /proc/cmdline
if [ $? -eq 0 ]; then
	get_passwd
	if [ ${PASSWD} == "0" ]; then
		/tos/tlog -m "recovery-mode, and don't change passwd"
	else 
		/tos/tlog -m "recovery-mode, and change passwd"
		echo -n -e "${PASSWD}\n${PASSWD}\n" | chroot $ROOT passwd
	fi
	/tos/tlog -m "recovery-mode, remove /boot/*backup-recovery*"
	rm -fv $ROOT/boot/*backup-recovery*
else
	get_passwd
        if [ ${PASSWD} == "0" ]; then
            #change password
            /tos/tlog -m "change passwd to Tlinux2019++"
            echo -n -e "Tlinux2019++\nTlinux2019++\n" | chroot $ROOT passwd
        else
                /tos/tlog -m "change passwd"
                echo -n -e "${PASSWD}\n${PASSWD}\n" | chroot $ROOT passwd
        fi
	if [ -z ${PASSWD} ]; then
	    #change password
	    /tos/tlog -m "change passwd to Tlinux2019++"
	    echo -n -e "Tlinux2019++\nTlinux2019++\n" | chroot $ROOT passwd
        fi
fi

grep -q "tlinux-64bit-v2" /proc/cmdline || grep -q "tlinux-64bit-v3" /proc/cmdline
if [ $? -eq 0 ]
then
       #chroot $ROOT chage -d 0 root
	true
fi
if [ -f $ROOT/boot/grub2/grub.cfg ]; then
        # grub2
        #mdadm --detail-platform | grep -q "Rapid Storage Technology enterprise"
        #if [ $? -eq 0 ]; then
        #        echo "modify /etc/default/grub!"
        #        chroot $ROOT mdadm --detail --scan --verbose > $ROOT/etc/mdadm.conf
        #        for i in `cat $ROOT/etc/mdadm.conf | egrep -o [:0-9a-zA-Z]{35}`;do
        #                sed -i "s/console=tty0/console=tty0 rd.md.uuid=${i}/" $ROOT/etc/default/grub
        #        done
        #fi
        chroot $ROOT rm -rf /boot/initramfs*
        /tos/tlog -m "mkinitrd, waiting"
        kern_ver=`ls $ROOT/boot/vmlinuz* | tail -n1 | cut -d "-" -f 2-`
        chroot $ROOT dracut -f /boot/initramfs-${kern_ver}.img ${kern_ver}
        /tos/tlog -m "mkinitrd done!"

        chroot $ROOT /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
        chroot $ROOT /usr/sbin/grub2-install /dev/${INSDISK}

	# for secureboot
        #if [ -d /sys/firmware/efi -a -f $ROOT/boot/efi/EFI/tencent/grubx64.efi ]; then
        #       cp /tos/grubx64.efi.signed $ROOT/boot/efi/EFI/tencent/grubx64.efi
	#	chmod 755 $ROOT/boot/efi/EFI/tencent/grubx64.efi
        #fi
	# end for secureboot

        #defkernel=$(grep -iR "\$menuentry_id_option" $ROOT/boot/grub2/grub.cfg | awk -F\' '/menuentry/{print $2}' | head -n1);
        chroot $ROOT /usr/sbin/grub2-set-default 0
else
        chroot $ROOT grub-install /dev/${INSDISK}
        if [ -d /sys/firmware/efi -a -f $ROOT/boot/efi/EFI/tencent/grub.efi ]; then
                chroot $ROOT efibootmgr -c -d /dev/${INSDISK} -p 2 -l \\EFI\\tencent\\grub.efi -L tencent
        fi
fi

if [ -f /tos/img/hardinstall_extra.rpm ]; then
	/tos/tlog -m "Install extra rpm"
	cp /tos/img/hardinstall_extra.rpm $ROOT
        chroot $ROOT /bin/rpm -Uvh hardinstall_extra.rpm
	rm $ROOT/hardinstall_extra.rpm
fi

if [ -f /tos/img/hardinstall_rc.local ]; then
	/tos/tlog -m "Append extra rc.local"
        cat /tos/img/hardinstall_rc.local >> $ROOT/etc/rc.d/rc.local
fi
if [ -f /tos/img/hardinstall_script ]; then
	/tos/tlog -m "run extra script in side chroot"
	cp /tos/img/hardinstall_script $ROOT
	chmod +x $ROOT/hardinstall_script
	chroot $ROOT /hardinstall_script
	rm $ROOT/hardinstall_script
	fuser -k -9 $ROOT
fi

# umount all /mnt/harddisk mounted if mounted
mount | grep "$ROOT" | awk '{print $3}' | sort -r | xargs umount

/tos/tlog -m "Grub installation done"
